package com.bh.weapp.controller.common;

import com.bh.common.annotation.RepeatSubmit;
import com.bh.common.config.BhtmsConfig;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.domain.entity.SysDictData;
import com.bh.common.core.domain.entity.SysDictType;
import com.bh.common.utils.StringUtils;
import com.bh.common.utils.file.FileUploadUtils;
import com.bh.common.utils.file.FileUtils;
import com.bh.framework.config.ServerConfig;
import com.bh.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单Controller
 *
 * @author slx
 * @date 2023-04-22
 */
@Api(tags = {"COMMON公共方法API"})
@RestController
@RequestMapping("/api/common")
public class JcCommonApiController extends BaseController {
    private static final String FILE_DELIMETER = ",";
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询订单列表
     */
    @ApiOperation("字典列表查询")
    @RepeatSubmit
    @PostMapping("/getDictAll")
    public AjaxResult getDictAll() {
        //需要转换为 JcOrderVo
        List<SysDictType> types = dictTypeService.selectDictTypeAll();
        // new
        Map<String, List<SysDictData>> datas = new HashMap<>();
        for (SysDictType type : types) {
            datas.put(type.getDictType(), dictTypeService.selectDictDataByType(type.getDictType()));
        }
        List<SysDictType> jcTypes = dictTypeService.selectJcDictTypeAll();
        for (SysDictType type : jcTypes) {
            datas.put(type.getDictType(), dictTypeService.selectJcDictDataByType(type.getDictType()));
        }
        return AjaxResult.success(datas);
    }


    /**
     * 通用上传请求（单个）
     */
    @ApiOperation("通用上传请求（单个）")
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, String type) {
        try {
            // 上传文件路径
            String filePath = BhtmsConfig.getUploadPath();
            // 上传并返回新文件名称
            if (StringUtils.isNotBlank(type)) {

            }
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 通用上传请求（多个）
     */
    @ApiOperation("通用上传请求（多个）")
    @PostMapping("/uploads")
    public AjaxResult uploadFiles(List<MultipartFile> files, String type) {
        try {
            // 上传文件路径
            String filePath = BhtmsConfig.getUploadPath();
            List<String> urls = new ArrayList<String>();
            List<String> fileNames = new ArrayList<String>();
            List<String> newFileNames = new ArrayList<String>();
            List<String> originalFilenames = new ArrayList<String>();
            for (MultipartFile file : files) {
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                String url = serverConfig.getUrl() + fileName;
                urls.add(url);
                fileNames.add(fileName);
                newFileNames.add(FileUtils.getName(fileName));
                originalFilenames.add(file.getOriginalFilename());
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("urls", StringUtils.join(urls, FILE_DELIMETER));
            ajax.put("fileNames", StringUtils.join(fileNames, FILE_DELIMETER));
            ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER));
            ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER));
            return ajax;
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
